package com.aun.hassels.validators;

import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.owasp.esapi.ESAPI;

/**
 * Custom validator to check the email address.
 * 
 * @author mdillic
 */
@FacesValidator("com.aun.hassels.validators.EmailInputValidator")
public class EmailInputValidator implements Validator {

	/**
	 * Checks the input string by using the ESAPI (Enterprise Security API) framework.
	 * 
	 * @see validation.properties
	 */
	@Override
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		org.owasp.esapi.Validator instance = ESAPI.validator();

		if (!instance.isValidInput(context.toString(), value.toString(), "Email", 600, true)) {
			ResourceBundle bundle = ResourceBundle.getBundle("i18n.validator.validator");

			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, bundle.getString("email.format"), bundle.getString("email.format"));

			throw new ValidatorException(message);
		}
	}

}
